﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace Infinity.Expressions
{
	/// <summary>
	/// Elágazások könnyebb készítésére lehet használni. 
	/// </summary>
	public partial class ConditionBuilder
	{
		protected Expression _Test;

		protected BlockBuilder _IfBlock;
		protected BlockBuilder _ElseBlock;

		protected ConditionalExpression ConditionExpression { get; set; }

		protected void RefreshCondition()
		{
			ConditionExpression = ConditionExpression.Update(_Test, _IfBlock, _ElseBlock);
		}

		public Expression Test
		{
			get
			{
				return _Test;
			}
			set
			{
				_Test = value;
				RefreshCondition();
			}
		}

		public BlockBuilder IfBlock
		{
			get
			{
				return _IfBlock;
			}
			set
			{
				_IfBlock = value;
				RefreshCondition();
			}
		}

		public BlockBuilder ElseBlock
		{
			get
			{
				return _ElseBlock;
			}
			set
			{
				_ElseBlock = value;
				RefreshCondition();
			}
		}

		public ConditionBuilder()
		{
			_Test = Expression.Constant(true);
			_IfBlock = new BlockBuilder();
			_ElseBlock = new BlockBuilder();
			ConditionExpression = Expression.Condition(_Test, _IfBlock, _ElseBlock);
		}

		public void Clear()
		{
			_IfBlock.Clear();
			_ElseBlock.Clear();
			_Test = Expression.Constant(true);
			RefreshCondition();
		}
	}
}
